import React from 'react';
import { Input, Tree, Row } from 'antd';
import config from './../../../config/config';

const { Search } = Input;

const CompanyTree = (props) => {

  const { companyList, companyLoading, onSearchCompany, onSelectTreeNode, onDelete, currentPage,
    onExpandList, pageSize, unfoldCollapseKeys } = props;

  const generateTreeData = (companyList) => {
    buildCompanyTree(companyList);
    return companyList;
  }
  const buildCompanyTree = (companyList) => {
    for (let i = 0; i < companyList.length; i++) {
      const record = companyList[i];
      let unitTypeObj = config.UNIT_TYPE.find(item => item.key == record.type);
      record.key = record.id;
      record.title =
        <div>
          <span style={{ background: unitTypeObj.color }}>{unitTypeObj.value}</span>
          <span style={{ marginLeft: 10 }}>{record.name}</span>
        </div>;

      let children = record.children;
      if (children) {
        buildCompanyTree(children);
      }
    }
  }

  return (
    <div>
      <Row style={{ padding: "0px 0px 15px 0px" }}>
        <Search placeholder="请输入单位名称" onSearch={onSearchCompany} enterButton style={{ width: "95%" }} />
      </Row>
      <Tree
        style={{ height: window.innerHeight - 70, overflowY: "scroll" }}
        defaultExpandAll={true}
        onSelect={onSelectTreeNode}
        treeData={generateTreeData(companyList)}
      />
    </div>
  );
};

export default CompanyTree;
